﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;


namespace lxwmvc
{
    /// <summary>
    /// 布局信息
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class layoutAttribute : Attribute
    {
        /// <summary>
        /// 布局构造函数，默认布局为default
        /// </summary>
        /// <param name="name"></param>
        public layoutAttribute(string name = "default")
        { layoutname = name; }
        /// <summary>
        /// 布局名称
        /// </summary>
        public string layoutname { get; set; }
    }

    ///// <summary>
    ///// 记录日志信息
    ///// </summary>
    //[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    //public class logAttribute : Attribute
    //{
    //    /// <summary>
    //    /// 布局构造函数，
    //    /// </summary>
    //    /// <param name="msg"></param>
    //    public logAttribute(string msg = "")
    //    { this.msg = msg; }
    //    /// <summary>
    //    /// 日志内容
    //    /// </summary>
    //    public string msg { get; set; }
    //}

    /// <summary>
    /// 别名
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class biemingAttribute : Attribute
    {
        /// <summary>
        /// 别名构造函数
        /// </summary>
        /// <param name="name"></param>
        public biemingAttribute(string name)
        { bieming = name; }
        /// <summary>
        /// 别名
        /// </summary>
        public string bieming { get; set; }
    }

    /// <summary>
    /// 支持事务
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class transAttribute : Attribute
    {
        /// <summary>
        /// 事务级别
        /// </summary>
        public IsolationLevel iso { get; set; }
        /// <summary>
        /// 是否构造函数
        /// </summary>
        /// <param name="iso">默认事务级别是 IsolationLevel.ReadCommitted</param>
        public transAttribute(IsolationLevel iso = IsolationLevel.ReadCommitted)
        {
            this.iso = iso;
        }
    }

    /// <summary>
    /// 支持锁定
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class tongbuAttribute : Attribute
    {
        /// <summary>
        /// 方法是否支持同步
        /// </summary>
        public bool tongbu { get; set; }

        /// <summary>
        /// 同步构造函数，默认支持同步
        /// </summary>
        /// <param name="tongbu"></param>
        public tongbuAttribute(bool tongbu = true)
        {
            //锁定
            this.tongbu = tongbu;
        }
    }

    /// <summary>
    /// 帮助类
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class helperAttribute : Attribute
    {
        /// <summary>
        /// 输出的模板中的类型
        /// </summary>
        public Type helpertype { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type"></param>
        public helperAttribute(Type type)
        {
            helpertype = type;
        }
        
        object _instance = null;
        /// <summary>
        /// 帮助类的实例
        /// </summary>
        public object instance
        {
            get
            {
                if (_instance == null)
                    _instance = Activator.CreateInstance(helpertype);
                return _instance;
            }
        }
    }

    /// <summary>
    /// 微信
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class weixinAttribute : Attribute
    {
        /// <summary>
        /// 输出的模板中的类型
        /// </summary>
        public Type type { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type"></param>
        /// <param name="url"></param>
        /// <param name="scope"></param>
        public weixinAttribute(Type type, string url, string scope = "snsapi_userinfo")
        {
            this.type = type;
            this.url = url;
            this.scope = scope;
        }

        IWeiXin _instance = null;
        /// <summary>
        /// 帮助类的实例
        /// </summary>
        public IWeiXin instance
        {
            get
            {
                if (_instance == null)
                    _instance = (IWeiXin)Activator.CreateInstance(type);
                return _instance;
            }
        }

        /// <summary>
        /// 回调处理地址
        /// </summary>
        public string url { get; set; }

        /// <summary>
        /// scope
        /// </summary>
        public string scope { get; set; }
    }

    /// <summary>
    /// 微信验证
    /// </summary>
    public interface IWeiXin
    {
        /// <summary>
        /// 微信验证
        /// </summary>
        /// <returns></returns>
        bool CheckUserInfo(string url,string scope);
    }

    /// <summary>
    /// 菜单属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class caidanAttribute : Attribute
    {
        /// <summary>
        /// 菜单构造函数
        /// </summary>
        /// <param name="mingcheng">菜单中文名称</param>
        public caidanAttribute(string mingcheng)
        {
            //初始化菜单信息
            this.mingcheng = mingcheng;
        }


        ///// <summary>
        ///// 菜单构造函数
        ///// </summary>
        //[Obsolete("已经过期，不能使用",true)]
        //public caidanAttribute()
        //{ 
            
        //}
        /// <summary>
        /// 菜单标示
        /// </summary>
        public string biaoshi { get; set; } //@canshu@list
        /// <summary>
        /// 显示的中文名称
        /// </summary>
        public string mingcheng { get; set; } //系统参数
        /// <summary>
        /// 路径
        /// </summary>
        public string path { get; set; } //http://localhost:80/canshu/list
    }

    /// <summary>
    /// 权限属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class quanxianAttribute : Attribute
    {
        /// <summary>
        /// 权限构造函数
        /// </summary>
        /// <param name="mingcheng">权限名称</param>
        /// <param name="caidan">所属菜单方法的名称 例如：save  例如:xxx.save</param>
        public quanxianAttribute(string mingcheng,string caidan)
        {
            //生成菜单
            this.caidan = caidan;//"@"+caidan.Method.ReflectedType.Name + "@" + caidan.Method.Name;
            
            this.mingcheng = mingcheng; 
        }
        ///// <summary>
        ///// 构造函数
        ///// </summary>
        //[Obsolete("已经过期，不能使用",true)]
        //public quanxianAttribute()
        //{  }
        /// <summary>
        /// 权限名称
        /// </summary>
        public string mingcheng { get; set; }

        /// <summary>
        /// 权限标示
        /// </summary>
        public string biaoshi { get; set; }
        
        /// <summary>
        /// 菜单标示
        /// </summary>
        public string caidan { get; set; }
        
        ///// <summary>
        ///// 验证权限和菜单是否符合要求
        ///// </summary>
        ////public void checkvalidate()
        ////{
        ////    if (!caidan.StartsWith("@", StringComparison.OrdinalIgnoreCase)) 
        ////        throw new Exception("006:菜单ID必须是'@'开头的字符串");
        ////    if (biaoshi.Split('@').Length != 3)
        ////        throw new Exception("007:权限ID必须是'@CCC@AAA'规则的字符串");
        ////}
    }

    /// <summary>
    /// 可以依赖菜单 依赖权限
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class yilaiAttribute : Attribute
    {
        /// <summary>
        /// 依赖权限
        /// </summary>
        /// <param name="quanxian_caidan">权限的方法名称 例如：save  例如:xxx.save</param>
        public yilaiAttribute(string quanxian_caidan)
        {
            //Action<pagebase> quanxian
            //this.quanxian = "@" + quanxian.Method.ReflectedType.Name + "@" + quanxian.Method.Name;
            this.quanxian_caidan = quanxian_caidan;
        }
        /// <summary>
        /// 权限名称
        /// </summary>
        public string quanxian_caidan { get; set; }

        ///// <summary>
        ///// 检查依赖权限是否符合要求
        ///// </summary>
        //public void checkvalidate()
        //{
        //    if (!
        //        (quanxian.StartsWith("@", StringComparison.OrdinalIgnoreCase))
        //        ) throw new Exception("004:权限ID必须是'@'开头的字符串");
        //    if (quanxian.Split('@').Length != 3) throw new Exception("005:权限ID必须是'@CCC@AAA'规则的字符串");
        //}
    }

    /// <summary>
    /// 输出的json对象
    /// </summary>
    public class BaseJson
    {
        /// <summary>
        /// 返回数据是否成功
        /// </summary>
        public bool success = true;
        /// <summary>
        /// 返回编码信息
        /// </summary>
        public int code = 200;
        /// <summary>
        /// 返回提示信息
        /// </summary>
        public string msg = "";
        /// <summary>
        /// 返回数据信息
        /// </summary>
        public object data = null;
    }

    /// <summary>
    /// 发送json对象
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class jsonAttribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public Type jsontype { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        public jsonAttribute(Type type = null)
        {
            if (type != null)
                jsontype = type;
            else
                jsontype = typeof(BaseJson);
        }


        object _instance = null;
        /// <summary>
        /// 
        /// </summary>
        public object instance
        {
            get
            {
                if (_instance == null)
                    _instance = Activator.CreateInstance(jsontype);
                return _instance;
            }
        }
    }
}
